<template>
	<view>
		<u-form :model="baseInfo" ref="uForm">
			<u-form-item label="Account" label-width="150" prop="account">
				<u-input v-model="baseInfo.account" disabled placeholder="Please enter account" />
			</u-form-item>
			<u-form-item label="Name" label-width="150" prop="name">
				<u-input v-model="baseInfo.name" placeholder="Please enter name" />
			</u-form-item>
			<u-form-item label="Phone Number" label-width="150" prop="phone">
				<u-input v-model="baseInfo.phone" placeholder="Please enter phone number" />
			</u-form-item>
			<u-button @click="tijiao" type="primary">Submit</u-button>
		</u-form>
	</view>
</template>

<script>
		export default {
			data() {
				return {
					baseInfo:{
						userId:'',
						account:'',
						name:'',
						sex:'',
						phone:'',
					}
				}
			},
			onShow() {
				this.getBaseInfo();
			},
			onPullDownRefresh() {
				this.getBaseInfo()
				uni.stopPullDownRefresh()
			},
			methods: {
				getBaseInfo(){
					this.$http.post('/rest/auth/me',{
					},{
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						const data = res.data.data
						this.baseInfo.account = data.account
						this.baseInfo.name = data.name
						this.baseInfo.phone = data.phone
					}).catch(err => {
						let message = err.data.message;
						this.$refs.uToast.show({
							title: 'Login expired, please log in again',
							type: 'error', 
							position:'bottom'
						})
						setTimeout(() => {
							uni.removeStorage({
								key: 'token',
								success: function (res) {
									
								},
								complete:function(res){
									uni.reLaunch({
										url: 'pages/login/login'
									});
								}
							});
						}, 500)
					});
				},
				tijiao(){
					if (typeof this.baseInfo.name == 'undefined' || this.baseInfo.name == null || this.baseInfo.name == '') {
						uni.showToast({
							title: 'Please enter name',
							duration: 2000,
							icon: 'none',
						});
						return false;
					}
					if (typeof this.baseInfo.phone == 'undefined' || this.baseInfo.phone == null || this.baseInfo.phone == '') {
						uni.showToast({
							title: 'Please enter phone number',
							duration: 2000,
							icon: 'none',
						});
						return false;
					}
					
					uni.showLoading({
						title: 'Submitting...'
					})
					this.$http.post('/rest/auth/changeBaseInfo', {}, {
						params: this.baseInfo,
						header: {
							'Authorization': uni.getStorageSync('token'),
						}
					}).then(res => {
						uni.hideLoading()
						if (res.data.code == 200) {
							this.getBaseInfo();
							uni.showToast({
								title: 'Operation successful',
								duration: 2000,
								icon: 'none',
							});
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 2000,
								icon: 'none',
							});
						}
					})
					
				}
			}
		}
</script>

<style>
</style>